<!DOCTYPE html>
<html class="x-admin-sm">

<head>
    <meta charset="UTF-8">
    <title>欢迎页面-X-admin2.2</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi"/>
    <link rel="stylesheet" href="../../css/font.css">
    <link rel="stylesheet" href="../../css/xadmin.css">
    <link rel="stylesheet" type="text/css" href="../../lib/layui/css/login.css" />
    <script src="../../lib/layui/layui.js" charset="utf-8"></script>
    <script type="text/javascript" src="../../js/xadmin.js"></script>
    <!--[if lt IE 9]>
    <script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
    <script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
</head>

<body>
<div class="x-nav">
			<span class="layui-breadcrumb">
            <a href="../index.html">首页</a>
            <a>
              <cite>用户</cite></a>
          </span>
    <a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right"
       onclick="location.reload()" title="刷新">
        <i class="layui-icon layui-icon-refresh" style="line-height:30px"></i></a>
</div>
<div style="padding-top: 15px">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-header">
                    <span>用户表</span>
                </div>
                <div class="layui-card-body ">
                    <table class="layui-hide" id="userTable" lay-filter="userTable"></table>
                    <script type="text/html" id="barDemo">
                        <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
                        <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
                    </script>
                </div>
            </div>
        </div>
    </div>
</div>
<script>
    layui.use(['laydate', 'form', 'laypage', 'layer', 'table', 'element', 'carousel'], function () {
        var laydate = layui.laydate;
        var form = layui.form;
        var laypage = layui.laypage;
        var layer = layui.layer;
        var table = layui.table;
        var element = layui.element;
        var carousel = layui.carousel;
        var $=layui.jquery;
        var add,edit;
        var role;

        form.render();

        //实例化一个table
        table.render({
            elem: '#userTable'
            ,id:'userTable'
            ,url: 'http://localhost:8080/user/list' //数据接口
            , title: '用户表'
            , height:360
            , page: true //开启分页
            , toolbar: 'default' //开启工具栏，此处显示默认图标，可以自定义模板，详见文档
            ,defaultToolbar:['filter', 'exports']
            , cols: [[ //表头
                {type: 'radio', fixed: 'left'
                }, {field: 'id', title: 'ID', width: '10%', sort: true, fixed: 'left',
                }, {field: 'username', title: '账号', width: '10%', sort: true
                }, {field: 'loginRole', title: '登录角色', hide: true
                }, {field: 'realname', title: '姓名', width: '10%', sort: true
                }, {field: 'telphone', title: '手机', width: '15%', minWidth: 100, sort: true
                }, {field: 'email', title: '邮箱', width: '25%', sort: true
                }, {field: 'role', title: '角色', width: '10%', minWidth: 80, sort: true
                },
                {fixed: 'right', width: '15%', minWidth: 100, align: 'center', toolbar: '#barDemo'
                }
            ]]
            ,done:function (res, curr, count){
                var userList=res.data;
                if(userList.length!=0){
                    role=userList[0].loginRole;
                }
            }
        });

        //监听头工具栏事件
        table.on('toolbar(userTable)', function (obj) {
            if(role!='管理员'){
                layer.msg('只能管理员添加用户，当前角色：'+role, {
                    icon: 5,
                    time:2000
                });
            }else {
                var checkStatus = table.checkStatus(obj.config.id),
                    data = checkStatus.data; //获取选中的数据
                switch (obj.event) {
                    case 'add':
                        //add用于获取弹层，便于后面的关闭
                        add = layer.open({
                            type: 1,
                            title: '添加用户',
                            area: ['600px'],
                            content: $('#admin_add')
                        });
                        break;
                    case 'update':
                        if (data.length === 0) {
                            layer.msg('请选择一行');
                        } else if (data.length > 1) {
                            layer.msg('只能同时编辑一个');
                        } else {
                            //初始值
                            $("#username_edit").val(data[0].username);
                            $("#realname_edit").val(data[0].realname);
                            edit = layer.open({
                                type: 1,
                                title: '修改用户',
                                area: ['600px'],
                                content: $('#admin_edit')
                            });
                        }
                        break;
                    case 'delete':
                        if (data.length === 0) {
                            layer.msg('请选择一行');
                        } else if (data.length > 1) {
                            layer.msg('只能同时编辑一个');
                        } else {
                            layer.confirm('是否删除当前行', function (index) {
                                obj.deleteData(obj.config.id); //删除对应行（tr）的DOM结构
                                layer.close(index);
                                //向服务端发送删除指令
                                $.ajax({
                                    url: 'http://localhost:8080/user/delete',
                                    data: data,
                                    contentType: "application/x-www-form-urlencoded",
                                    type: 'POST',
                                    xhrFields: {
                                        withCredentials: true
                                    },
                                    success: function (data) {
                                        if (data.status == "success") {
                                            layer.msg('删除成功', {
                                                icon: 1,
                                                time: 1000
                                            }, function () {
                                                //添加用户成功后关闭弹窗并刷新
                                                layer.close(index);
                                                table.reload('userTable', {
                                                    url: 'http://localhost:8080/user/list'
                                                })
                                            });
                                        } else {
                                            layer.msg(data.data.errMsg, {
                                                icon: 5,
                                                time: 1000
                                            });
                                        }
                                    }
                                });
                            });
                        }
                        break;
                }
                ;
            }
        });

        //监听行工具事件
        table.on('tool(userTable)', function (obj) { //注：tool 是工具条事件名，test 是 table 原始容器的属性 lay-filter="对应的值"
            if(role!='管理员'){
                layer.msg('只能管理员管理用户，当前角色：'+role, {
                    icon: 5,
                    time:2000
                });
            }else {
                var data = obj.data; //获得当前行数据
                var layEvent = obj.event; //获得 lay-event 对应的值
                if (layEvent === 'del') {
                    layer.confirm('删除后将不可恢复', {icon: 3, title: '提示'}, function (index) {
                        layer.close(index);
                        $.ajax({
                            url: 'http://localhost:8080/user/delete',
                            data: data,
                            contentType: "application/x-www-form-urlencoded",
                            type: 'POST',
                            xhrFields: {
                                withCredentials: true
                            },
                            success: function (data) {
                                if (data.status == "success") {
                                    obj.del();
                                    layer.msg('删除成功', {
                                        icon: 1,
                                        time: 1000
                                    }, function () {
                                        //添加用户成功后关闭弹窗并刷新
                                        layer.close(index);
                                        table.reload('userTable', {
                                            url: 'http://localhost:8080/user/list'
                                        })
                                    });
                                } else {
                                    layer.msg(data.data.errMsg, {
                                        icon: 5,
                                        time: 1000
                                    });
                                }
                            }
                        });
                    });
                }
                else if (layEvent === 'edit') {
                    $("#username_edit").val(data.username);
                    $("#realname_edit").val(data.realname);
                    edit = layer.open({
                        type: 1,
                        title: '修改用户',
                        area: ['600px'],
                        content: $('#admin_edit')
                    });
                }
            }
        });

        form.on('submit(register-submit)', function(obj) {
            var field = obj.field;
            //获取复选框中的角色信息
            var arr=new Array()
            $("input:checkbox[name=role]:checked").each(function(){
                arr.push($(this).val());
            });
            //将数组合并成字符串
            field.role=arr.join(",");
            $.ajax({
                url: 'http://localhost:8080/user/register',
                data: field,
                contentType: "application/x-www-form-urlencoded",
                type: 'POST',
                xhrFields: {
                    withCredentials: true
                },
                success: function(data) {
                    if(data.status == "success") {
                        layer.msg('注册成功', {
                            icon: 1,
                            time: 1000
                        }, function() {
                            //添加用户成功后关闭弹窗并刷新
                            layer.close(add);
                            table.reload('userTable',{
                                url:'http://localhost:8080/user/list'
                            })
                        });
                    } else {
                        layer.msg(data.data.errMsg, {
                            icon: 5,
                            time:1000
                        });
                    }
                },
                error: function(xhr) {
                    console.log('error');
                    console.log(xhr);
                    layer.msg('请按F12查看控制台信息', {
                        icon: 5,
                        time:1000
                    });
                }
            });
        });

        form.on('submit(edit-submit)',function (obj) {
            var field = obj.field;
            //获取复选框中的角色信息
            var arr=new Array()
            $("input:checkbox[name=role_edit]:checked").each(function(){
                arr.push($(this).val());
            });
            //将数组合并成字符串
            field.role_edit=arr.join(",");

            $.ajax({
                url: 'http://localhost:8080/user/update',
                data: field,
                contentType: "application/x-www-form-urlencoded",
                type: 'POST',
                xhrFields: {
                    withCredentials: true
                },
                success: function(data) {
                    if(data.status == "success") {
                        layer.msg('修改成功', {
                            icon: 1,
                            time: 1000
                        }, function() {
                            //添加用户成功后关闭弹窗并刷新
                            layer.close(edit);
                            table.reload('userTable',{
                                url:'http://localhost:8080/user/list'
                            })
                        });
                    } else {
                        layer.msg(data.data.errMsg, {
                            icon: 5,
                            time:1000
                        });
                    }
                },
                error: function(xhr) {
                    console.log('error');
                    console.log(xhr);
                    layer.msg('请按F12查看控制台信息', {
                        icon: 5,
                        time:1000
                    });
                }
            });
        });
    });
</script>
</body>
<div style="background-color: lightgray;display: none;" id="admin_add">
    <div class="login-body">
        <div class="layui-card">
            <div class="layui-card-header">
                添加用户
            </div>
            <div class="layui-card-body layui-form layui-form-pane"  style="width: 300px">
                <!--用户名-->
                <div class="layui-form-item">
                    <label class="layui-form-label"><i class="layui-icon layui-icon-username"></i></label>
                    <div class="layui-input-inline">
                        <input name="username" type="text" required lay-verify="required" autocomplete="off" placeholder="用户名" class="layui-input">
                    </div>
                    <div class="layui-form-mid" style="color: red;">*</div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label"><i class="layui-icon layui-icon-username"></i></label>
                    <div class="layui-input-inline">
                        <input name="realname" type="text" required lay-verify="required" autocomplete="off" placeholder="姓名" class="layui-input">
                    </div>
                    <div class="layui-form-mid" style="color: red;">*</div>
                </div>
                <!--密码-->
                <div class="layui-form-item">
                    <label class="layui-form-label"><i class="layui-icon layui-icon-password"></i></label>
                    <div class="layui-input-inline">
                        <input id="password" name="password" type="password" required lay-verify="required" autocomplete="off" placeholder="密码" class="layui-input">
                    </div>
                    <div class="layui-form-mid" style="color: red;">*</div>
                </div>
                <!--手机号-->
                <div class="layui-form-item">
                    <label class="layui-form-label"><i class="layui-icon layui-icon-cellphone"></i></label>
                    <div class="layui-input-inline">
                        <input name="telphone" type="text" required lay-verify="required|phone|number" autocomplete="off" placeholder="手机号" class="layui-input">
                    </div>
                    <div class="layui-form-mid" style="color: red;">*</div>
                </div>
                <!--邮箱-->
                <div class="layui-form-item">
                    <label class="layui-form-label"><i class="layui-icon layui-icon-read"></i></label>
                    <div class="layui-input-inline">
                        <input name="email" type="text" required lay-verify="required|email" autocomplete="off" placeholder="邮箱" class="layui-input">
                    </div>
                    <div class="layui-form-mid" style="color: red;">*</div>
                </div>
                <!--选定角色-->
                <div class="layui-form-item">
                    <label class="layui-form-label"><i class="layui-icon layui-icon-note"></i></label>
                    <div class="layui-input-block">
                        <input type="checkbox" name="role" lay-skin="primary" title="项目" value="项目经理" checked="">
                        <input type="checkbox" name="role" lay-skin="primary" title="开发" value="开发人员">
                        <input type="checkbox" name="role" lay-skin="primary" title="测试" value="测试人员">
                    </div>
                </div>
                <!--提交注册-->
                <div class="layui-form-item">
                    <button lay-filter="register-submit" class="layui-btn layui-btn-fluid layui-btn-radius" lay-submit>立即提交</button>
                </div>
            </div>
        </div>
    </div>
</div>
<div style="background-color: lightgray;display: none;" id="admin_edit">
    <div class="login-body">
        <div class="layui-card">
            <div class="layui-card-header">
                修改用户
            </div>
            <div class="layui-card-body layui-form layui-form-pane"  style="width: 300px" lay-filter="edit-form">
                <!--用户名-->
                <div class="layui-form-item">
                    <label class="layui-form-label"><i class="layui-icon layui-icon-username"></i></label>
                    <div class="layui-input-inline">
                        <input name="username_edit" id="username_edit" type="text" class="layui-input" title="用户名不可修改" disabled>
                    </div>
                    <div class="layui-form-mid" style="color: red;">*</div>
                </div>
                <!--姓名-->
                <div class="layui-form-item">
                    <label class="layui-form-label"><i class="layui-icon layui-icon-username"></i></label>
                    <div class="layui-input-inline">
                        <input name="realname_edit" id="realname_edit" type="text" required lay-verify="required" autocomplete="off" placeholder="姓名" class="layui-input">
                    </div>
                    <div class="layui-form-mid" style="color: red;">*</div>
                </div>
                <!--选定角色-->
                <div class="layui-form-item">
                    <label class="layui-form-label"><i class="layui-icon layui-icon-note"></i></label>
                    <div class="layui-input-block">
                        <input type="checkbox" name="role_edit" lay-skin="primary" value="项目经理" title="项目" checked>
                        <input type="checkbox" name="role_edit" lay-skin="primary" value="开发人员" title="开发">
                        <input type="checkbox" name="role_edit" lay-skin="primary" value="测试人员" title="测试">
                    </div>
                </div>
                <!--提交注册-->
                <div class="layui-form-item">
                    <button lay-filter="edit-submit" class="layui-btn layui-btn-fluid layui-btn-radius" lay-submit>提交修改</button>
                </div>
            </div>
        </div>
    </div>
    <div class="login-footer">
        <p>© 2019 </p>
    </div>
</div>
</html>